﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.ComponentModel;
using System.Globalization;
using System.Web.Configuration;

namespace HospitalSystem
{
    public partial class preference : System.Web.UI.Page
    {
        public static List<string> x = new List<string>();
        DataTable dt = new DataTable();
        DataTable dx = new DataTable();
        
        protected void Page_Load(object sender, EventArgs e)
        {

            String ShowValue = (string)Session["CID"];
            Label1.Text = ShowValue;
            String ShowValue2 = (string)Session["LastName"];
            Label3.Text = ShowValue2;

            String ShowValue3 = (string)Session["FirstName"];
            Label2.Text = ShowValue3;


            String ShowValue4 = (string)Session["CaseNo"];
            Label4.Text = ShowValue4;
             if (!IsPostBack)
            {
                BindData();
            }
             if (Session["CaseNo"] == null)
             {

                 MessageBox("กรุณากดค้นหา หรือลงทะเบียนการคัดกรองเด็กก่อน", "checkcid.aspx");

             }


        }

        private void MessageBox(string msg, string urld)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "');" + "window.location.href='" + urld + "';" + "</script>";
            Page.Controls.Add(lbl);
        }
        protected void BindData()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            x.Clear();
            scon.Open();
            SqlCommand cmd = new SqlCommand("select Name from FavorItem", scon);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read() != false)
            {
                x.Add(reader["Name"].ToString());
            }
            reader.Close();
            dx.Columns.Add("Name");

            for (int i = 0; i < x.Count(); i++)
            {
                dx.Rows.Add();
                dx.Rows[i]["Name"] = x[i].ToString();
            }
            GridView1.DataSource = dx;
            GridView1.DataBind();
            scon.Close();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            
            if (dt.Columns.Count <= 0)
            {
                dt.Columns.Add("CID");
                dt.Columns.Add("CaseNo");
                dt.Columns.Add("FavorItemCode");
                dt.Columns.Add("FavorItemDescription");
                dt.Columns.Add("RecorderID");
                dt.Columns.Add("EvalDateTime");
            }
            foreach (GridViewRow row in GridView1.Rows)
            {
                
                
                scon.Open();
                SqlCommand cmd = new SqlCommand("select FavorItemCode from FavorItem where Name = @sj", scon);
                cmd.Parameters.AddWithValue("@sj", GridView1.Rows[row.RowIndex].Cells[0].Text);

                TextBox cb = (TextBox)row.FindControl("FID");
                if (cb != null && cb.Text != string.Empty)
                {
                    SqlDataReader reader2 = cmd.ExecuteReader();
                    //selectorChecked = true;
                    //string CaseNo = GridView1.Rows[row.RowIndex].Cells[0].Text;
                    //SelectStat.Text += string.Format("{0} has been added <br />", CaseNo);

                    //for (int i = 0; i < x.Count(); i++)
                    //{
                    if (reader2.Read())
                    {
                        dt.Rows.Add(Label1.Text, Label4.Text, reader2["FavorItemCode"].ToString(), cb.Text, Label5.Text, DateTime.Now.ToString());
                    }
                    else
                    {
                    }
                    //dt.Rows[row.RowIndex]["CID"] = TextBox6.Text ;
                    //dt.Rows[row.RowIndex]["CaseNo"] = TextBox7.Text;
                    //dt.Rows[row.RowIndex]["SightEvalCode"] = reader2["SightEvalCode"].ToString();
                    //dt.Rows[row.RowIndex]["RecorderID"] = TextBox8.Text;
                    //dt.Rows[row.RowIndex]["SightEvalCode"] = DateTime.Now.ToString();
                    //}
                }
                scon.Close();
            }
            GridView2.DataSource = dt;
            GridView2.DataBind();

              SqlCommand command22 = new SqlCommand("delete from [FavorList] where CID=@CID and CaseNo=@CaseNo", scon);
            scon.Open();
            command22.Parameters.AddWithValue("@CID", Label1.Text);
            command22.Parameters.AddWithValue("@CaseNo", Label4.Text);
            command22.ExecuteNonQuery();
            scon.Close();
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                SqlCommand command = new SqlCommand("INSERT INTO [FavorList] (CID,CaseNo,FavorItemCode,FavorItemDescription,RecorderID,EvalDateTime) VALUES('" + dt.Rows[i]["CID"].ToString() + "','" + dt.Rows[i]["CaseNo"].ToString() + "','" + dt.Rows[i]["FavorItemCode"].ToString() + "','" + dt.Rows[i]["FavorItemDescription"].ToString() + "','" + dt.Rows[i]["RecorderID"].ToString() + "','" + dt.Rows[i]["EvalDateTime"].ToString() + "')", scon);
               scon.Open();

              command.ExecuteNonQuery();
               scon.Close();
           }
        }
    }
}